import { createRouter, createWebHistory } from 'vue-router';
import { ElMessage } from 'element-plus';

const routes = [
  // 主页面
  {
    path: '/',
    name: 'home',
    label: '首页',
    component: () => import('@/views/HomeView.vue'),
    redirect: '/user/userlist',
    children: [
      {
        path: 'user',
        name: 'user',
        label: '角色权限',
        redirect: '/user/userlist',
        component: () => import('@/views/user/userManageIndexView.vue'),
        children: [
          {
            path: 'userlist',
            name: 'userlist',
            label: '用户管理',
            component: () => import('@/views/user/userManageListView.vue'),
          },
          {
            path: 'departmanage',
            name: 'departmanage',
            label: '部门管理',
            component: () => import('@/views/user/departmentManage.vue'),
          },
          {
            path: 'rolemanage',
            name: 'rolemanage',
            label: '角色管理',
            component: () => import('@/views/user/roleManage.vue'),
          },
        ],
      },
      // 客户相关
      {
        path: 'client',
        name: 'client',
        label: '客户管理',
        redirect: '/client/clientlist',
        component: () => import('@/views/production/clientIndexView.vue'),
        children: [
          {
            path: 'clientlist',
            name: 'clientlist',
            label: '客户列表',
            component: () => import('@/views/production/clientListView.vue'),
          },
          {
            path: 'clientlv',
            name: 'clientlv',
            label: '成长等级',
            component: () => import('@/views/production/clientLevelView.vue'),
          },
        ],
      },
      {
        path: 'ConfirmView',
        name: 'ConfirmView',
        component: () => import('@/views/production/ConfirmView.vue'),
      },
      // 商品列表
      {
        path: 'prolist',
        name: 'prolist',
        label: '商品列表',
        component: () => import('@/views/production/ProductionListView.vue'),
      },
      {
        path: 'prorecomend',
        name: 'prorecomend',
        label: '商品推荐',
        component: () =>
          import('@/views/production/ProductionRecomentView.vue'),
      },
      // {
      //   path: 'ConfirmView',
      //   name: 'ConfirmView',
      //   component: () => import('@/views/production/ConfirmView.vue'),
      // },
      {
        path: 'banner',
        name: 'banner',
        label: '商品管理',
        component: () => import('@/views/banner/BannerIndexView.vue'),
        children: [
          {
            path: 'bannerlist',
            name: 'bannerlist',
            label: '轮播图管理',
            component: () => import('@/views/banner/BannerListView.vue'),
          },
          {
            path: 'ProductReviews',
            name: 'ProductReviews',
            label: '商品评论管理',
            component: () => import('@/views/banner/ProductReviews.vue'),
          },
        ],
      },
    ],
  },
  // 登录
  {
    path: '/login',
    name: 'login',
    label: '登录',
    component: () => import('../views/Login.vue'),
    redirect: '/login/loginview',
    children: [
      {
        path: 'loginview',
        name: 'loginview',
        label: '登录',
        component: () => import('../views/login/loginView.vue'),
      },
      {
        path: 'register',
        name: 'register',
        label: '注册',
        component: () => import('../views/login/registerView.vue'),
      },
    ],
  },
  // 个人中心(用户页面)
];
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes,
});

const isLogin = () => {
  return !!sessionStorage.getItem('token'); // 如果token为''|undefined|false等，返回结果都是false
};
// 前置路由守卫的判断
router.beforeEach((to) => {
  if (to.name === 'loginview') {
    if (isLogin()) {
      ElMessage({
        type: 'error',
        message: '已经登录，无需重复登录！',
      });
      return { name: 'home' };
    }
  } else if (to.name !== 'register') {
    if (!isLogin()) {
      ElMessage({
        type: 'error',
        message: '请登录后查看！',
      });
      return { name: 'loginview' };
    }
  }
});
export { routes };
export default router;
